package org.edu.modules.sbzc.place.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.sbzc.place.entity.VeSbzcCdInfo;
import org.edu.modules.sbzc.place.mapper.VeSbzcCdApplyMapper;
import org.edu.modules.sbzc.place.entity.VeSbzcCdApply;
import org.edu.modules.sbzc.place.dto.VeSbzcCdApplyDto;
import org.edu.modules.sbzc.place.service.VeSbzcCdApplyService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.sbzc.place.service.VeSbzcCdInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 实现类
 */
@Service
public class VeSbzcCdApplyServiceImpl extends BaseServiceImpl<VeSbzcCdApply, VeSbzcCdApplyMapper> implements VeSbzcCdApplyService {

    @Autowired
    private  VeSbzcCdInfoService sbzcCdInfoService;

    public IPage<VeSbzcCdApplyDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeSbzcCdApplyDto findDtoById(String id) {
        VeSbzcCdApply entity = super.findById(id);
        VeSbzcCdApplyDto dto = ConvertUtils.convertToDto(entity, VeSbzcCdApplyDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeSbzcCdApplyDto dto,Boolean isAdd){
        if(isAdd){
            LambdaUpdateWrapper<VeSbzcCdInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(VeSbzcCdInfo::getIsAppliable,0);
            updateWrapper.eq(VeSbzcCdInfo::getId,dto.getCdId());
            sbzcCdInfoService.update(updateWrapper);
            super.insert(dto);
        }else{
            super.update(dto);
        }
    }

}
